package com.zzedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzThesaurusTag;
import com.zzedu.dto.ZzThesaurusTagDTO;
import com.zzedu.service.IZzThesaurusTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 标签管理Controller
 *
 * @author Mr.Wang
 * @date 2025-01-26
 */
@RestController
@RequestMapping("/thesaurus/tag")
public class ZzThesaurusTagController extends BaseController {
    @Autowired
    private IZzThesaurusTagService zzThesaurusTagService;

    /**
     * 查询标签管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzThesaurusTagDTO zzThesaurusTag) {
        return getDataTable(zzThesaurusTagService.page(zzThesaurusTag.getPageDTO(), zzThesaurusTagService.buildQuery(zzThesaurusTag)));
    }

    /**
     * 导出标签管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:export')")
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzThesaurusTagDTO zzThesaurusTag) {
        List<ZzThesaurusTag> list = zzThesaurusTagService.list(zzThesaurusTagService.buildQuery(zzThesaurusTag));
        ExcelUtil<ZzThesaurusTag> util = new ExcelUtil<ZzThesaurusTag>(ZzThesaurusTag.class);
        util.exportExcel(ServletUtils.getResponse(), list, "标签管理数据");
    }


    /**
     * 新增保存标签管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:add')")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@RequestBody ZzThesaurusTag zzThesaurusTag) {
        return toAjax(zzThesaurusTagService.save(zzThesaurusTag));
    }


    /**
     * 获取标签管理详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzThesaurusTagService.getById(id));
    }


    /**
     * 修改保存标签管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:edit')")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@RequestBody ZzThesaurusTag zzThesaurusTag) {
        return toAjax(zzThesaurusTagService.updateById(zzThesaurusTag));
    }

    /**
     * 删除标签管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:tag:remove')")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzThesaurusTagService.removeBatchByIds(ids));
    }


    /**
     * 未关联标签列表
     */
    @RequestMapping("/noRelatedList")
    public TableDataInfo noRelatedList(@RequestBody ZzThesaurusTagDTO zzThesaurusTag) {
        LambdaQueryWrapper<ZzThesaurusTag> lambdaQueryWrapper = zzThesaurusTagService.buildQuery(zzThesaurusTag);
        lambdaQueryWrapper.apply("id not in(select tag_id from zz_thesaurus_words_tag_related where deleted=0 and word_id={0})", zzThesaurusTag.getWordId());
        return getDataTable(zzThesaurusTagService.page(zzThesaurusTag.getPageDTO(), lambdaQueryWrapper));
    }

    /**
     * 已关联标签列表
     */
    @RequestMapping("/relatedList")
    public TableDataInfo relatedList(@RequestBody ZzThesaurusTagDTO zzThesaurusTag) {
        LambdaQueryWrapper<ZzThesaurusTag> lambdaQueryWrapper = zzThesaurusTagService.buildQuery(zzThesaurusTag);
        lambdaQueryWrapper.apply("id in(select tag_id from zz_thesaurus_words_tag_related where deleted=0 and word_id={0})", zzThesaurusTag.getWordId());
        return getDataTable(zzThesaurusTagService.page(zzThesaurusTag.getPageDTO(), lambdaQueryWrapper));
    }
}
